#!/bin/bash
source /home/kjj-18/my_shell/func.sh

#收取邮件
getmail --rcfile=getmaillrc.264693247

echo "/***************mail fetch finished, mail process begin!!******************/"

#指定邮件处理目录和邮件收取目录
cur_date=$(date +%Y年%m月%d日)
#upload_rec_dir="/media/kjj-18/文档/何一航文档/科技局工作/#局网站管理/@上传记录"
upload_rec_dir="/media/share/upload"
readonly upload_rec_dir
mail_dir="/home/kjj-18/.getmail/maildir/new/"
readonly mail_dir 

#检查以当前日期命名的目录是否存在
rec_exist=$(find ${upload_rec_dir} -name ${cur_date} | wc -l)
#echo ${rec_exist}

#检查已经收取了多少邮件
if [ $rec_exist -eq 0 ]; then
    received_mail_num=0
else
    received_mail_num=$(ls ${upload_rec_dir}"/"${cur_date} -l | grep "^d" | wc -l)
fi
echo "old_mail_num is $received_mail_num"

start_line=0
end_line=0
grep_ret=0
index=0
unreceived_mail_num=0
subject=""
content=""
tmp_str=""
for file in `ls $mail_dir`; do  #遍历邮件收取目录下的邮件文件
    #echo $file
    if [ $# -eq 0 ]; then       #没有输入参数时，搜索关键字"syskjj@126.com"
        tmp_str=$(grep 'From: "syskjj@126.com" <syskjj@126.com>' ${mail_dir}"/"${file})
    else                        #有输入参数时，以输入参数作为关键字搜索
        for i in "$@"; do
            #echo $i
            tmp_str=$(grep $i ${mail_dir}"/"${file})
            if [ $? -eq 0 ]; then break; fi
        done
    fi

    grep_ret=$?
    #echo "grep_ret is ${grep_ret}"
    if [ $grep_ret -eq 0 ]; then    #搜索到关键字，提取邮件的主题、内容、附件，并处理附件文件
        #创建以当前日期命名的目录
        if [ $rec_exist -eq 0 ]; then 
            mkdir ${upload_rec_dir}"/"${cur_date} 
            rec_exist=1
        fi
        let "unreceived_mail_num++"
	    index=`expr $received_mail_num + $unreceived_mail_num`	

        #解析邮件主题
        subject=$(parse_subject ${mail_dir}"/"${file})
        echo "mail No."${index}": $subject"
        cur_dir=${upload_rec_dir}"/"${cur_date}"/"${index}"-"${subject}
        #echo $cur_dir
        tmp_dir=${cur_dir}"/tmp"
        unpack_dir=${tmp_dir}"/unpack"
        src_dir=${tmp_dir}"/src"
        cmprs_dir=${tmp_dir}"/compress_file"
	    mkdir ${cur_dir}
        mkdir ${tmp_dir}
        mkdir ${unpack_dir}
        mkdir ${src_dir}

        #解析邮件正文
        parse_content ${mail_dir}"/"${file}>${cur_dir}"/content.txt"
        correct_mistake ${cur_dir}"/content.txt"
        #echo "mail content: $content"

        #echo "create directory ${cur_dir} success!!"; 
        mv ${mail_dir}"/"${file} ${tmp_dir} 
	    if [ $? -eq 0 ]; then 
            #echo "move to temp directory success!!"; 
            munpack -t -C ${unpack_dir} ${tmp_dir}"/"$file > /dev/null
            cd ${unpack_dir}
            rm ${unpack_dir}"/part1"
            rm ${unpack_dir}"/part2"
            for src_name in `find . -type f`; do
                #echo $src_name
                dst_name=$(parse_filename ${src_name#*/})
                #echo $dst_name
                if [[ $(file_type ${dst_name}) = "cmprs" ]]; then   #压缩文件特别处理
                    cmprs_exist=$(find ${tmp_dir} -name "compress_file" | wc -l)
                    if [ ${cmprs_exist} -eq 0 ]; then mkdir ${cmprs_dir}; fi
                    cp ${unpack_dir}"/"${src_name} ${cmprs_dir}"/"${dst_name}
                    uncompress ${cmprs_dir}"/"${dst_name} ${src_dir}
                else
                    cp ${unpack_dir}"/"${src_name} ${src_dir}"/"${dst_name}
                fi
            done

            img_index=0
            pdf_index=0
            doc_index=0
            img_dir=""
            img_in_doc_index=0
            cd ${src_dir}
            for file_name in `ls`; do
                if [[ $(file_type ${file_name}) = "img" ]]; then
                    let "img_index++"
                    convert ${file_name} -resize 800 ${cur_dir}"/"${file_name%.*}".jpg"
                elif [[ $(file_type ${file_name}) = "pdf" ]]; then
                    let "pdf_index++"
                    convert ${file_name} -resize 800 -density 300 ${cur_dir}"/"${file_name%.*}".png"
                elif [[ $(file_type ${file_name}) = "doc" ]]; then
                    let "doc_index++"
                    libreoffice --convert-to txt ${file_name} > /dev/null
                    cp ${file_name%.*}".txt" ${cur_dir}"/"${doc_index}"-"${file_name%.*}".txt"
                    correct_mistake ${cur_dir}"/"${doc_index}"-"${file_name%.*}".txt"
                    if [ ${file_name##*.} != "docx" ]; then
                        libreoffice --convert-to docx ${file_name} > /dev/null
                    fi
                    img_dir=$(unzip_docx ${file_name%.*}".docx" $doc_index)
                    if [ ${#img_dir} -ne 0 ]; then 
                        for img_name in `ls $img_dir`; do
                            let "img_in_doc_index++"
                            img_name=$img_dir"/"$img_name
                            convert $img_name -resize 800 ${cur_dir}"/doc-"${doc_index}"-pic-"${img_in_doc_index}".jpg"
                        done
                    fi
                else
                    cp ${file_name} ${cur_dir}
                fi
            done
        else
            echo "mv mail file failed..."
        fi

    elif [ $grep_ret -eq 1 ]; then  #没有搜索到关键字，将邮件丢弃
        mv ${mail_dir}"/"${file} "/home/kjj-18/.getmail/maildir/trash/"
	    if [ $? -eq 0 ]; then echo "move to "/home/kjj-18/.getmail/maildir/trash/" success!!"; fi
    fi
done
echo "new_mail_num is ${unreceived_mail_num}"
echo "total mail num is $(expr $received_mail_num + $unreceived_mail_num)"

#if [ ${unreceived_mail_num} -ne 0 ]; then gnome-open ${upload_rec_dir}"/"${cur_date}; fi
if [ ${unreceived_mail_num} -ne 0 ]; then 
    firefox > /dev/null &
    sleep 1
    terminator --working-directory ${upload_rec_dir}"/"${cur_date} > /dev/null 
fi

